#include "common.h"

#define ERR_NULL_INPUT -1
#define ERR_ILL_LINE   -2

#define CORRECT_LINE 0
#define USELESS_LINE 1

extern unsigned int check_interval;
extern bool always_show_tray_icon;
extern char *email_client;
extern struct Maildir *maildirs;
extern char *new_mail_icon;
extern char *default_icon;

static int 
parse_line (const char *line, char **key, char **value)
{
	char *a, *b;

	if (line == NULL)
		return ERR_NULL_INPUT;

	/* Skip useless lines */
	if ((line[0] == '#') || (line[0] == '\n')) 
		return USELESS_LINE;
	if (!(a = strchr (line, '='))) 
		return ERR_ILL_LINE;

	/* overwrite '=' with '\0' */
	a[0] = '\0';
	*key = strdup (line);
	a++;

	/* overwrite '\n' with '\0' if '\n' present */
	if ((b = strchr (a, '\n'))) b[0] = '\0';

	*value = strdup (a);

	g_strstrip(*key);
	g_strstrip(*value);

	fprintf(stdout, "key = %s\nvalue = %s\n\n", *key, *value);

	return CORRECT_LINE;
}

int
parse_config(const char *config_path)
{
	FILE *config_fp = NULL;
	char line[1024];
	int line_no;
	char *key = NULL , *value = NULL;
	int retval;
	struct Maildir *last_maildir;
	bool maildir_path_toggle;

	if (config_path == NULL) {
		fprintf(stderr, "ERR: configure file path is NULL.\n");
		return -1;
	}

	if ((config_fp = fopen(config_path, "r")) == NULL) {
		fprintf(stderr, 
			"ERR: failed to open configure file %s.\n", 
			config_path);
		return -1;
	}

	line_no = 0;
	last_maildir = maildirs;
	maildir_path_toggle = FALSE;
	while ((fgets(line, sizeof(line), config_fp)) != NULL) {
		retval = parse_line(line, &key, &value);
		line_no++;
		if (retval == ERR_NULL_INPUT) {
			fprintf(stderr, "ERR: line == NULL\n");
			continue;
		} else if (retval == ERR_ILL_LINE) {
			fprintf(stderr, 
				"ERR: not found \'=\' in %s:%d.\n",
				config_path, line_no);
			continue;
		} else if (retval == USELESS_LINE) {
			DEBUG_MSG("empty line or comment.");
			continue;
		}
		
		if (key == NULL || !strcmp(key, "")) {
			fprintf(stderr,
				"ERR: empty key in %s:%d.\n",
				config_path, line_no);
			if (key != NULL) {
				free(key); key = NULL;
			}
			if (value != NULL) {
				free(value); value = NULL;
			}
			continue;
		}

		if (value == NULL || !strcmp(value, "")) {
			fprintf(stderr,
				"ERR: empty value in %s:%d.\n",
				config_path, line_no);
			if (key != NULL) {
				free(key); key = NULL;
			}
			if (value != NULL) {
				free(value); value = NULL;
			}
			continue;
		}

		if (!strcmp(key, "check_interval")) {
			check_interval = atoi(value);
		} else if (!strcmp(key, "always_show_tray_icon")) {
			if (!strcasecmp(value, "true") ||
			    !strcasecmp(value, "yes")) {
				always_show_tray_icon = TRUE;
			} else if (!strcasecmp(value, "false") ||
				!strcasecmp(value, "no")) {
				always_show_tray_icon = FALSE;
			} else {
				fprintf(stderr, 
					"ERR: need a boolean value at %s:%d.\n",
					config_path, line_no);
			}
		} else if (!strcmp(key, "email_client")) {
			email_client = strdup(value);
		} else if (!strcmp(key, "maildir_path")) {
			struct Maildir *new_maildir = (struct Maildir *)
				malloc(sizeof(struct Maildir));
			if (new_maildir == NULL) {
				fprintf(stderr, 
					"ERR: memory allocation failed.\n");
				maildir_path_toggle = FALSE;
				goto parse_line_end;
			}
			
			new_maildir->path = strdup(value);
			new_maildir->descr = NULL;
			new_maildir->next_maildir = NULL;
			if (last_maildir != NULL)
				last_maildir->next_maildir = new_maildir;
			else
				maildirs = new_maildir;
			last_maildir = new_maildir;

			maildir_path_toggle = TRUE;
		} else if (!strcmp(key, "maildir_desc")) {
			if (maildir_path_toggle == FALSE) {
				fprintf(stderr, 
					"ERR: unmatched \'maildir_desc\' at %s:%d.\n",
					config_path, line_no);
				goto parse_line_end;
			}

			last_maildir->descr = strdup(value);
			maildir_path_toggle = FALSE;
		} else if (!strcmp(key, "new_mail_icon")) {
			if (access(value, R_OK))
				fprintf(stderr,
					"ERR: failed to open %s.\n",
					value);
			new_mail_icon = strdup(value);
		} else if (!strcmp(key, "default_icon")) {
			if (access(value, R_OK))
				fprintf(stderr,
					"ERR: failed to open %s.\n",
					value);
			default_icon = strdup(value);
		} else {
			fprintf(stderr, 
				"ERR: unrecognized key \'%s\' at %s:%d.\n",
				key, config_path, line_no);
		}

	parse_line_end:
		free(key); key = NULL;
		free(value); value = NULL;
	}


	fclose(config_fp);
	return 0;
}
